﻿Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Script.Services
Imports System.Web.Services.Protocols
Imports VSModel
Imports System.Data.Objects
Imports Cls_Common

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class wsAutoComplete
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetListAutoCompleteProduct(ByVal strSrc As String) As List(Of String)
        Dim json As New List(Of String)
        Dim strConnect As String = ""
        Using data As New VSEntities
            'Load du lieu ung voi ky tu nhap vao

            Dim p = (From m In data.Products Where m.Code.Contains(strSrc) Or m.Title.Contains(strSrc) Or m.QueryString.Contains(strSrc) _
                     Order By m.Title Ascending Select m).ToList

            'Export du lieu sang kieu du lieu json
            If Not p Is Nothing AndAlso p.Count > 0 Then
                For i As Integer = 0 To p.Count - 1
                    json.Add(p(i).Code.ToString & " - " & p(i).Title.ToString)
                Next
            End If
        End Using
        Return json.ToList()
    End Function

    <WebMethod()> _
      <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function SearchProduct(ByVal strSrc As String) As List(Of String)
        Dim json As New List(Of String)
        Dim strConnect As String = ""
        Using data As New VSEntities
            'Load du lieu ung voi ky tu nhap vao
            Dim abc = makeURLFriendly(strSrc)
            Dim p = (From m In data.Products Where m.Title.Contains(strSrc) Or m.QueryString.Contains(abc) Or m.Code.Contains(strSrc) _
                     Order By m.Title Ascending Select m).ToList

            'Export du lieu sang kieu du lieu json
            If Not p Is Nothing AndAlso p.Count > 0 Then
                For i As Integer = 0 To p.Count - 1
                    If Not p(i).Title Is Nothing Then
                        json.Add(p(i).Code.ToString & " - " & p(i).Title.ToString)
                    Else
                        json.Add(p(i).Code.ToString)
                    End If

                Next
            End If
        End Using
        Return json.ToList()
    End Function


    'Generate OrderNumber

    <WebMethod(EnableSession:=True)> _
 <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function GenerateOrderNumber(ByVal sSACode As String) As String
        Session("curSA") = sSACode
        Dim dicSA As New Dictionary(Of String, String)

        'create session lưu trữ lại SACode khi bấm chọn ddlSA  Then
        If Not Session("ListGenPO") Is Nothing Then
            dicSA = CType(Session("ListGenPO"), Dictionary(Of String, String))
            If dicSA.ContainsKey(sSACode) Then
                Return dicSA(sSACode)
            End If
        End If

        Dim result As String
        Using data As New VSEntities
            Dim k = (From a In data.Stores Where a.StoreId = sSACode Select a).FirstOrDefault
            'neu ngay hien tai khac ngay luu trong database thi reset sequence = 1
            If (k.OrderDate <> Now.Date) Or k.OrderDate Is Nothing Then
                k.DailyOrder = 1
                k.OrderDate = Now.Date
                data.SaveChanges()
            Else
                k.DailyOrder = k.DailyOrder + 1
                data.SaveChanges()
            End If
            result = k.StoreId.ToString("0#") & "-" & Now.Date.ToString("yyMMdd") & "-" & CType(k.DailyOrder, Integer).ToString("00#")

            'lưu vào dic
            dicSA(sSACode) = result
            Session("ListGenPO") = dicSA

        End Using
        Return result
    End Function


    <WebMethod(EnableSession:=True)> _
    Public Function checkLogin(ByVal sUserName As String, ByVal sPassword As String) As Integer
        Dim strEncryptPass As String = Encrypt(sPassword)
        Using _data As New VSEntities
            Dim p As User = (From q In _data.Users Where (q.UserName.Trim = sUserName Or q.Password.Trim = sUserName) And q.Password = strEncryptPass).SingleOrDefault
            If (Not p Is Nothing) Then
                Session("UserName") = p.UserName
                Session("UserId") = p.UserId
                Session("UserType") = p.IsUser
                p.LastLogin = Now
                'If IsNothing(p.NgayThamGia) Then
                '    p.NgayThamGia = Now()
                'End If
                _data.SaveChanges()

                Return 1
            End If
        End Using
        Return 0
    End Function

    <WebMethod(EnableSession:=True)> _
    Public Function Logout() As Integer

        Session("UserName") = Nothing
        Session("UserId") = Nothing
        Session("UserType") = Nothing

        Return 1

    End Function
End Class